package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import csv_parser.CSVToMatrix;

import neural_network.CommandRunner;

public class ReadyStatePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JButton classify, back;
	JTextField alpha_min, alpha_max, f_alpha_min, f_alpha_max, d_max, q;

	private JButton clear;

	public ReadyStatePanel() {
		BorderedLabel label = new BorderedLabel(
				"Klasifikator je spreman. Učitajte uzorak.",
				new Color(0xBDECB6), 18);
		this.add(label);
		JPanel container = new JPanel();
		GridLayout gl;
		container.setLayout(gl = new GridLayout(6, 2));
		gl.setHgap(10);
		gl.setVgap(10);
		int fieldSize = 10;
		JLabel label1 = new JLabel("D_max");
		container.add(label1);
		container.add(d_max = new JTextField(fieldSize));
		RoundedCornerBorder border = new RoundedCornerBorder();
		d_max.setBorder(border);
		d_max.addKeyListener(new ValidationAction());
		
		container.add(new JLabel("Q"));
		container.add(q = new JTextField(fieldSize));
		q.setBorder(new RoundedCornerBorder());
		q.addKeyListener(new ValidationAction());
		
		container.add(new JLabel("f(alpha)_min"));
		container.add(f_alpha_min = new JTextField(fieldSize));
		f_alpha_min.setBorder(new RoundedCornerBorder());
		f_alpha_min.addKeyListener(new ValidationAction());
		
		container.add(new JLabel("alpha_f_min"));
		container.add(alpha_min = new JTextField(fieldSize));
		alpha_min.setBorder(new RoundedCornerBorder());
		alpha_min.addKeyListener(new ValidationAction());
		
		container.add(new JLabel("f(alpha)_max"));
		container.add(f_alpha_max = new JTextField(fieldSize));
		f_alpha_max.addKeyListener(new ValidationAction());
		f_alpha_max.setBorder(new RoundedCornerBorder());
		
		container.add(new JLabel("alpha_f_max"));
		container.add(alpha_max = new JTextField(fieldSize));
		alpha_max.setBorder(new RoundedCornerBorder());
		alpha_max.addKeyListener(new ValidationAction());
		
		this.add(container);
		JPanel randompanel = new JPanel();
		randompanel.setPreferredSize(new Dimension(240, 200));
		this.add(randompanel);
		this.classify = new JButton("Klasifikuj uzorak");
		this.classify.setEnabled(false);
		this.classify.addActionListener(new ClassifyAction());
		this.add(classify);
		this.clear = new JButton("Nova klasifikacija");
		this.clear.addActionListener(new ClearAction());
		this.add(clear);
		
		this.back = new JButton("Nazad na učitavanje podataka");
		this.back.addActionListener(new BackAction());
		this.add(back);
		
		this.setVisible(false);
	}

	public class ValidationAction implements KeyListener {

		private boolean isNumber(String text) {
			if (text.isEmpty())
				return false;
			boolean result = true;
			try {
				Float.parseFloat(text);
			} catch (NumberFormatException ex) {
				ex.printStackTrace();
				result = false;
			}
			return result;
		}

		@Override
		public void keyTyped(KeyEvent e) {

		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyReleased(KeyEvent e) {
			String text = d_max.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			text = q.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			text = f_alpha_min.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			text = alpha_min.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			text = f_alpha_max.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			text = alpha_max.getText();
			if (!isNumber(text)) {
				ReadyStatePanel.this.classify.setEnabled(false);
				return;
			}
			ReadyStatePanel.this.classify.setEnabled(true);
		}

	}

	public class ClassifyAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// sacuva parametre
			float data[] = new float[6];
//			StringBuilder sb = new StringBuilder();
			data[0] = Float.parseFloat(d_max.getText());
			data[1] = Float.parseFloat(q.getText());
			data[2] = Float.parseFloat(f_alpha_min.getText());
			data[3] = Float.parseFloat(alpha_min.getText());
			data[4] = Float.parseFloat(f_alpha_max.getText());
			data[5] = Float.parseFloat(alpha_max.getText());
			System.out.println(Arrays.toString(data));
			CSVToMatrix.normalize(data);
			System.out.println(Arrays.toString(data));
			data = CSVToMatrix.createInput(data);
			System.out.println(Arrays.toString(data));
			StringBuilder sb = CSVToMatrix.prepareData(data);
			System.out.println(Arrays.toString(data));
//			sb.append(d_max.getText() + ",");
//			sb.append(q.getText() + ",");
//			sb.append(f_alpha_min.getText() + ",");
//			sb.append(alpha_min.getText() + ",");
//			sb.append(f_alpha_max.getText() + ",");
//			sb.append(alpha_max.getText());
			String toString = sb.toString();
			System.out.println(toString);
			File file = new File("onekarcinom.m");
			try {
				PrintWriter writer = new PrintWriter(file);
				writer.append(toString);
				writer.close();
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}

			// pozove komandu
			CommandRunner
					.runCommand("octave metastaze_one.m no_pause no_graphic");
			if (true) {
				// read result
				Frame.getFrame().setReadyStatePanelVisible(false);
				Frame.getFrame().setResultPanelVisible(true);
			}
		}

	}

	public class ClearAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			d_max.setText("");
			q.setText("");
			f_alpha_min.setText(""); 
			alpha_min.setText("");
			f_alpha_max.setText("");
			alpha_max.setText("");
			classify.setEnabled(false);
		}
		
	}
	
	public class BackAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Frame.getFrame().setReadyStatePanelVisible(false);
			Frame.getFrame().setNotReadyStatePanelVisible(true);
		}
		
	}
}
